package com.voodoo.lib_mqtt.listener;

import com.voodoo.lib_logcat.L;
import com.voodoo.lib_mqtt.MqttOperation;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttException;

/**
 * Author：voodoo
 * CreateTime：2021/07/15 015 下午 05:45
 * Description：Mqtt连接监听
 */
public class MqttConnectionListener implements IMqttActionListener {

    MqttAndroidClient mqttAndroidClient;

    public MqttConnectionListener(MqttAndroidClient mqttAndroidClient) {
        this.mqttAndroidClient = mqttAndroidClient;
    }

    @Override
    public void onSuccess(IMqttToken asyncActionToken) {
        L.i("Mqtt链接成功");
        try {
            MqttOperation.getInstance().subscribeToTopic(mqttAndroidClient);
        } catch (MqttException e) {
            L.e("设置订阅消息异常：", e.toString());
        }
    }

    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
        L.e("Mqtt链接失败", asyncActionToken, exception);
    }

}
